package com.itboye.eweilisten.me.zhanghu;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.adapter.GuessLikeAdapter;
import com.itboye.eweilisten.adapter.OrderAdapter;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.base.BaseFragment;
import com.itboye.eweilisten.base.BasePtr;
import com.itboye.eweilisten.me.home.ActivityExperience;
import com.itboye.eweilisten.me.home.ActivityShopPlay;
import com.itboye.eweilisten.me.login.LoginActivity;
import com.itboye.eweilisten.model.GuessBooks;
import com.itboye.eweilisten.model.ZhangHuBean;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;


/**
 * 我的订单
 */
@SuppressLint("ValidFragment")
public class ZhangHuFragment extends BaseFragment implements Observer, OrderAdapter.AdapterOrderFace {
    OrderAdapter adapter;
    ListView listView;
    TextView tv_word;
    LinearLayout ll_guessLike;
    PtrFrameLayout commend_anchor_ptr;
    public static ZhangHuBean datas;
    List<ZhangHuBean.InfoList> list;
    static List<ZhangHuBean.InfoList> listall = new ArrayList<>();
    UserPresenter presenter;
    private int pgaeNum = 1;
    private int pageSize = 10;
    int type;
    String id;
    int ppp;
    ImageView img_kong;

    private GridView guesslike;
    private GuessLikeAdapter guessLikeAdapter1;
    private GuessBooks guessBooks1 = new GuessBooks();
    private List<GuessBooks.ListInfo> listGuessInfo1;

    public ZhangHuFragment(int type) {
        this.type = type;
    }

    @Override
    public int initView() {
        return R.layout.zhang_hu_fragment;
    }

    @Override
    public void onMyActivityCreated() {
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        datas = new ZhangHuBean();

        presenter = new UserPresenter(this);

        list = new ArrayList<>();
        adapter = new OrderAdapter(getActivity(), list, this);
        listView.setAdapter(adapter);

        listGuessInfo1 = new ArrayList<>();
        guessLikeAdapter1 = new GuessLikeAdapter(getActivity(), listGuessInfo1);
        guesslike.setAdapter(guessLikeAdapter1);

        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pgaeNum++;
                presenter.onOrder(IsStringSPUtils.getSid(), IsStringSPUtils.IsUid(), "", type + "", pgaeNum + "", pageSize + "");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.onOrder(IsStringSPUtils.getSid(), IsStringSPUtils.IsUid(), "", type + "", pgaeNum + "", pageSize + "");
            }
        });
        commend_anchor_ptr.autoRefresh();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ZhangHuDelie.class);
                intent.putExtra("orderCode", list.get(position).getOrder_code());
                intent.putExtra("status", list.get(position).getOrder_status());
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void onStart() {
        super.onStart();
        presenter.onOrder(IsStringSPUtils.getSid(), IsStringSPUtils.IsUid(), "", type + "", pgaeNum + "", pageSize + "");

        presenter.onGuessLike("3", "1", "16",IsStringSPUtils.IsUid(),IsStringSPUtils.getSid());//每页3个，第一页，16表示推荐书籍

    }

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);
        if (entity != null) {
            if (entity.getCode().equals("1111")) {
                Intent intent = new Intent(MyApplication.instance, LoginActivity.class);
                startActivity(intent);
                return;
            }
            if (entity.getEventType() == presenter.Order_success) {
                if (pgaeNum == 1) {
                    list.clear();
                    listall = list;
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                datas = (ZhangHuBean) entity.getData();
                list.addAll(datas.getList());
                listall = list;

                if (list.size() > 0) {
                    commend_anchor_ptr.setVisibility(View.VISIBLE);
                    listView.setVisibility(View.VISIBLE);
                    img_kong.setVisibility(View.GONE);
                    adapter.notifyDataSetChanged();
                } else {
                    commend_anchor_ptr.setVisibility(View.GONE);
                    listView.setVisibility(View.GONE);
                    img_kong.setVisibility(View.VISIBLE);
                }
            }
            if (entity.getEventType() == presenter.Order_fail) {
                MAlert.alert(entity.getData());
            }
            //取消订单
            if (entity.getEventType() == presenter.CanlerOrder_success) {
                MAlert.alert(entity.getData() + "");
                Iterator<ZhangHuBean.InfoList> iterator = list.iterator();
                while (iterator.hasNext()) {
                    ZhangHuBean.InfoList bean = iterator.next();
                    if (bean.getId().equals(id)) {
                        //  iterator.remove();
                        list.get(ppp).setQuery_status("8");
                        adapter.notifyDataSetChanged();
                        break;
                    }
                }
            }
            if (entity.getEventType() == presenter.guess_like_success) {
                guessBooks1 = (GuessBooks) entity.getData();
                listGuessInfo1.addAll(guessBooks1.getList());
                tv_word.setText("猜你喜欢");
                if (listGuessInfo1.size() > 0) {
                    guessLikeAdapter1.notifyDataSetChanged();
                    guesslike.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(getActivity(), ActivityExperience.class);
                            intent.putExtra("countment", guessBooks1.getList().get(position).getSecondary_headlines());
                            intent.putExtra("name", guessBooks1.getList().get(position).getName());
                            intent.putExtra("imgid", guessBooks1.getList().get(position).getMain_img());
                            intent.putExtra("id", guessBooks1.getList().get(position).getP_id());
                            startActivity(intent);
                        }
                    });
                }
            }
            if (entity.getEventType() == presenter.guess_like_fail) {
                MAlert.alert(entity.getData());
            }
            if (entity.getEventType() == presenter.CanlerOrder_fail) {
                MAlert.alert(entity.getData());
            }
        }
    }

    /**
     * @param view
     */
    @Override
    public void onConfirm(View view) {

    }

    @Override
    public void onCanler(View view) {

    }

    /**
     * 取消订单，
     */
    @Override
    public void onDelete(View view) {
        ppp = (Integer) view.getTag();
        id = list.get((Integer) view.getTag()).getId();
        presenter.onCanlerOrder(IsStringSPUtils.getSid(), IsStringSPUtils.IsUid(), list.get((Integer) view.getTag()).getOrder_code());
        adapter.notifyDataSetChanged();
    }

    /***
     * 立即支付
     * @param view
     */
    @Override
    public void onPliay(View view) {
        String price = list.get((Integer) view.getTag()).getPrice();
        double dbprice = Double.parseDouble(price);
        Intent intent = new Intent(getActivity(), ActivityShopPlay.class);
        intent.putExtra("price", dbprice / 100 + "");
        intent.putExtra("type", "chongxin");
        intent.putExtra("order_code", list.get((Integer) view.getTag()).getOrder_code());
        for (int i = 0; i < list.get((Integer) view.getTag()).getItems().size(); i++) {
            intent.putExtra("sku_pkid", list.get((Integer) view.getTag()).getItems().get(i).getPsku_id());
        }

        startActivity(intent);
    }
}
